package com.martinzhuang.welcomedemo;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

/**
 * Created by martinzhuang on 7/23/15.
 */
public class WelcomeActivity extends Activity implements AnimationListener {

    private ImageView imageView;
    private Animation alphaAnimation;
    private SharedPreferences.Editor editBoolean;
    private SharedPreferences getFirstUse;
    private Boolean isFirstUse;
    private View welcome;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //hide action bar
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //hide navigation bar
        welcome = getWindow().getDecorView();
        int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN;
        welcome.setSystemUiVisibility(uiOptions);

        setContentView(R.layout.welcome_activity);
        imageView = (ImageView)findViewById(R.id.welcome_image_view);
        alphaAnimation = AnimationUtils.loadAnimation(this, R.anim.welcome_alpha);
        alphaAnimation.setFillEnabled(true);
        alphaAnimation.setFillAfter(true);
        imageView.setAnimation(alphaAnimation);
        alphaAnimation.setAnimationListener(this);

        getFirstUse = getSharedPreferences("isFirstUse", MODE_WORLD_READABLE);
        editBoolean = getFirstUse.edit();

    }

    @Override
    public void onAnimationStart(Animation animation) {

    }

    @Override
    public void onAnimationEnd(Animation animation) {
        isFirstUse = getFirstUse.getBoolean("isFirstUse", true);
        if (isFirstUse){
            Intent intent = new Intent(WelcomeActivity.this, GuideView.class);
            startActivity(intent);
            this.finish();
            editBoolean.putBoolean("isFirstUse", false);
            editBoolean.commit();
        } else {
            Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
            startActivity(intent);
            this.finish();
        }
    }

    @Override
    public void onAnimationRepeat(Animation animation) {

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            return false;
        }
        return false;
    }
}
